package com.dts.evaluate.ui.activity;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dts.evaluate.APP;
import com.dts.evaluate.R;
import com.dts.evaluate.bean.PersonBean;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.ui.view.TitleView;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;

public class NatureInfoSureActivity extends BaseActivity implements View.OnClickListener,TitleView.OnBackClikListener {


    @Bind(R.id.title_view)
    TitleView titleView;
    @Bind(R.id.nature_textview1)
    TextView natureTextview1;
    @Bind(R.id.nature_textview2)
    TextView natureTextview2;
    @Bind(R.id.nature_textview3)
    TextView natureTextview3;
    @Bind(R.id.nature_textview4)
    TextView natureTextview4;
    @Bind(R.id.nature_textview5)
    TextView natureTextview5;
    @Bind(R.id.nature_textview6)
    TextView natureTextview6;
    @Bind(R.id.nature_textview7)
    TextView natureTextview7;
    @Bind(R.id.nature_textview8)
    TextView natureTextview8;
    @Bind(R.id.nature_connect_textview1)
    TextView natureConnectTextview1;
    @Bind(R.id.nature_connect_textview2)
    TextView natureConnectTextview2;
    @Bind(R.id.nature_connect_textview3)
    TextView natureConnectTextview3;
    @Bind(R.id.nature_connect_textview4)
    TextView natureConnectTextview4;
    @Bind(R.id.nature_connect_textview5)
    TextView natureConnectTextview5;
    @Bind(R.id.nature_connect_textview6)
    TextView natureConnectTextview6;
    @Bind(R.id.nature_connect_textview7)
    TextView natureConnectTextview7;
    @Bind(R.id.nature_connect_textview8)
    TextView natureConnectTextview8;
    @Bind(R.id.sure_textview)
    TextView sureTextview;
    @Bind(R.id.cancel_textview)
    TextView cancelTextview;
    private MediaPlayer mediaPlayer;
    private AssetFileDescriptor afd1;//对应信息确认提示mp3
    private AssetFileDescriptor afd2;//对应谢谢mp3

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nature_info_sure);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        PersonBean personBean = (PersonBean) intent.getSerializableExtra(Constants.PERSON_BEAN);

        //初始化控件
        initView(personBean);

        //初始化音乐文件和mediaPlayer
        afd1 = getResources().openRawResourceFd(R.raw.infoconfirm);
        afd2 = getResources().openRawResourceFd(R.raw.thank);
        mediaPlayer = new MediaPlayer();

        //播放 信息确认提示
        try {
            mediaPlayer.setDataSource(afd1.getFileDescriptor(), afd1.getStartOffset(), afd1.getLength());
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();

    }

    /**
     * 初始化控件
     * @param personBean
     */
    private void initView(PersonBean personBean){
        sureTextview.setOnClickListener(this);
        titleView.setOnBackClickListener(this);
        cancelTextview.setOnClickListener(this);

        if (personBean != null) {
            String male = getResources().getString(R.string.male);
            String female = getResources().getString(R.string.female);

            natureTextview1.setText(personBean.getPubUserName());
            int sex = personBean.getSex();
            String sexStr = sex == 1 ?  male: female;
            natureTextview2.setText(sexStr);
            natureTextview3.setText(personBean.getIdCard());
            natureTextview4.setText(personBean.getMobile());
            natureTextview5.setText(personBean.getEmail());
            natureTextview6.setText(personBean.getTel());
            natureTextview7.setText(personBean.getPostcode());
            natureTextview8.setText(personBean.getAddress());

            natureConnectTextview1.setText(personBean.getLinkmanName());
            sex = personBean.getLinkmanSex();
            sexStr = sex == 1 ? male : female;
            natureConnectTextview2.setText(sexStr);
            natureConnectTextview3.setText(personBean.getLinkmanIdCard());
            natureConnectTextview4.setText(personBean.getLinkmanMobile());
            natureConnectTextview5.setText(personBean.getLinkmanEmail());
            natureConnectTextview6.setText(personBean.getLinkmanTel());
            natureConnectTextview7.setText(personBean.getLinkmanPostcode());
            natureConnectTextview8.setText(personBean.getLinkmanPostAddres());
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sure_textview:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    mediaPlayer.stop();
                }
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(afd2.getFileDescriptor(), afd2.getStartOffset(), afd2.getLength());
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            finish();
                        }
                    });
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.cancel_textview:
                finish();
                break;

        }
    }


    @Override
    public void onTitleViewClik(View v) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        APP.sure_flag = false;
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mediaPlayer.stop();
            }
            mediaPlayer.release();
        }


    }


}
